package com.ai3d.rfid.base.fragment

import android.app.Activity
import android.graphics.drawable.AnimationDrawable
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.inputmethod.InputMethodManager
import androidx.databinding.ViewDataBinding
import com.ai3d.rfid.base.vm.BaseViewModel
import com.ai3d.rfid.ext.dismissLoadingExt
import com.ai3d.rfid.ext.inflateBindingWithGeneric
import com.ai3d.rfid.ext.showLoadingExt

/**
 *Base fragment injected with ViewModel and ViewBinding automatically
 * which is suited to someone who wants to use ViewDataBinding
 */
abstract class BaseVmVdbFragment<VM : BaseViewModel, VDB : ViewDataBinding> : BaseVmFragment<VM>() {

    var amyAnimation: AnimationDrawable? = null
    var baseHandler = Handler(Looper.getMainLooper()) {
        blink()
        false
    }
    private fun blink(){
//        if(null!=mDatabind.root.findViewById<ImageView>(R.id.iv_amy)) {
//            mDatabind.root.findViewById<ImageView>(R.id.iv_amy)
//                .setImageDrawable(resources.getDrawable(R.drawable.amy_blink_animation))
//            amyAnimation = mDatabind.root.findViewById<ImageView>(R.id.iv_amy).drawable as AnimationDrawable
//            amyAnimation!!.start()
//            baseHandler.sendEmptyMessageDelayed(0, 4 * 1000)
//        }
    }
    override fun layoutId() = 0

    //该类绑定的ViewDataBinding
    private var _binding: VDB? = null
    val mDatabind: VDB get() = _binding!!

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        _binding = inflateBindingWithGeneric(inflater, container, false)
        return mDatabind.root
    }

    override fun onDestroyView() {
        super.onDestroyView()
        baseHandler.removeCallbacksAndMessages(null)
        _binding = null
    }


    abstract override fun initView(savedInstanceState: Bundle?)

    /**
     *lazy loading data
     *this fun only triggered when this fragment is being displayed
     */
    override fun lazyLoadData() {}

    /**
     *Creating of LiveData observer triggered after #onViewCreated
     */
    override fun createObserver() {}

    /**
     *Triggered after #onViewCreated
     */
    override fun initData() {

    }

    /**
     *Show loading dialog
     */
    override fun showLoading(message: String) {
        showLoadingExt(message)
    }

    /**
     *Dismiss loading dialog
     */
    override fun dismissLoading() {
        dismissLoadingExt()
    }

    override fun onPause() {
        super.onPause()
        hideSoftKeyboard(activity)
    }

    /**
     * 延迟加载 防止 切换动画还没执行完毕时数据就已经加载好了，这时页面会有渲染卡顿  bug
     * 这里传入你想要延迟的时间，延迟时间可以设置比转场动画时间长一点 单位： 毫秒
     * 不传默认 300毫秒
     * @return Long
     */
    override fun lazyLoadTime(): Long {
        return 300
    }

    /**
     *Hide Soft Keyboard
     */
    fun hideSoftKeyboard(activity: Activity?) {
        activity?.let { act ->
            val view = act.currentFocus
            view?.let {
                val inputMethodManager =
                    act.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
                inputMethodManager.hideSoftInputFromWindow(
                    view.windowToken,
                    InputMethodManager.HIDE_NOT_ALWAYS
                )
            }
        }
    }

    /**
     *show Soft Keyboard
     */
    fun showSoftKeyboard(activity: Activity?) {
        activity?.let { act ->
            val view = act.currentFocus
            view?.let {
                val inputMethodManager =
                    act.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
                inputMethodManager.showSoftInput(
                    view,
                    InputMethodManager.HIDE_NOT_ALWAYS
                )
            }
        }
    }

}